package fun.stgoder.test1;

import java.util.concurrent.Exchanger;

public class TestExchanger {
    public static void main(String[] args) {

        Exchanger<String> exchanger = new Exchanger<>();

        new Thread(() -> {

            String s = "s1";
            System.out.println("t1 before exchange " + s);
            try {
                s = exchanger.exchange(s);
                System.out.println("t1 after exchange " + s);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }).start();

        new Thread(() -> {

            String s = "s2";
            System.out.println("t2 before exchange " + s);
            try {
                s = exchanger.exchange(s);
                System.out.println("t2 after exchange " + s);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }).start();
    }
}
